package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.entity.Result;
import com.itheima.pojo.Blog;
import com.itheima.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Description:
 *sql搜索相关接口
 * @author chenzhi
 * @date 2020/8/6
 */
@RestController
@RequestMapping("/search")
@CrossOrigin(allowCredentials="true", allowedHeaders="*", methods={RequestMethod.GET,RequestMethod.POST}, origins="*")
public class WebSearchController {

    @Autowired
    private BlogService blogService;

    //根据作者获取相关的博客 --> author
    @GetMapping("/searchBlogByAuthor")
    public Result searchBlogByAuthor(@RequestParam("author")String author,@RequestParam(name = "currentPage",required = false) Integer currentPage,@RequestParam(name = "pageSize",required = false) Integer pageSize){
        Page<Blog> blogContentList =  blogService.searchBlogByAuthor(author,currentPage,pageSize);
        com.itheima.entity.Page result = new com.itheima.entity.Page();
        result.setTotal((int) blogContentList.getTotal());
        result.setRecords(blogContentList.getResult());
        return new Result("success",result) ;


    }

    //根据博客分类UID获取相关的博客 -->  blogSortUid
    @GetMapping("/searchBlogBySort")
    public Result searchBlogBySort(@RequestParam("blogSortUid")String blogSortUid,@RequestParam(name = "currentPage",required = false) Integer currentPage,@RequestParam(name = "pageSize",required = false) Integer pageSize){

        Page<Blog> blogContentList =  blogService.searchBlogBySort(blogSortUid,currentPage,pageSize);
        com.itheima.entity.Page result = new com.itheima.entity.Page();
        result.setTotal((int) blogContentList.getTotal());
        result.setRecords(blogContentList.getResult());
        return new Result("success",result) ;

    }

    //根据标签uid获取相关的博客 -->  tagUid
    @GetMapping("/searchBlogByTag")
    public Result searchBlogByTag(@RequestParam("tagUid")String tagUid,@RequestParam(name = "currentPage",required = false) Integer currentPage,@RequestParam(name = "pageSize",required = false) Integer pageSize){

        Page<Blog> blogContentList =  blogService.getBlogContentListPage(tagUid,currentPage,pageSize);
        com.itheima.entity.Page result = new com.itheima.entity.Page();
        result.setTotal((int) blogContentList.getTotal());
        result.setRecords(blogContentList.getResult());
        return new Result("success",result) ;

    }

    //根据关键字查询Blog
    @GetMapping("/sqlSearchBlog")
    public Result sqlSearchBlog(@RequestParam("keyword")String keyword,@RequestParam(name = "currentPage",required = false) Integer currentPage,@RequestParam(name = "pageSize",required = false) Integer pageSize){

        Page<Blog> blogContentList =  blogService.sqlSearchBlog(keyword,currentPage,pageSize);
        com.itheima.entity.Page result = new com.itheima.entity.Page();
        result.setTotal((int) blogContentList.getTotal());
        result.setRecords(blogContentList.getResult());
        return new Result("success",result) ;
    }
}

